package media.plugins.timeline;

import javax.media.*;
 
 // import COM.yourbiz.media.EventPoster;
 
 // The implementation of the EventPoster class is not included as part
 // of this example. EventPoster supports two methods:
 // public EventPoster ();
 // public void postEvent (ControllerListener who, ControllerEvent
 //        what);
 
 public class EventPostingBase 
 {
     protected ListenerList olist;
     protected Object olistLock;
     protected EventPoster eventPoster;
     // We sync around a new object so that we don't mess with
     // the super class synchronization.

     EventPostingBase () 
     {
         olistLock = new Object ();
     }
 
     public void addControllerListener (ControllerListener observer) 
     {
         synchronized (olistLock) 
         {
             if (eventPoster == null) 
             {
                 eventPoster = new EventPoster ();
             }   
         
             ListenerList iter;
             for (iter = olist; iter != null; iter = iter.next) 
             {
                 if (iter.observer == observer) return;
             }
 
             iter = new ListenerList ();
             iter.next = olist;
             iter.observer = observer;
             olist = iter;  
         }   
     }
 
     public void removeControllerListener (ControllerListener  observer) 
     {
         synchronized (olistLock) 
         {
             if (olist == null) 
             {
                 return;
             } 
             else if (olist.observer == observer) 
             {
                 olist = olist.next;
             } 
             else 
             {
                 ListenerList iter;
                 for (iter = olist; iter.next != null; iter = iter.next) 
                 {
                     if (iter.next.observer == observer) 
                     {
                         iter.next = iter.next.next;

                         return;
                     }
                 }
             }
         }
     }
 
 
     protected void postEvent (ControllerEvent event) 
     {
         synchronized (olistLock) 
         {
             ListenerList iter;
             for (iter = olist; iter != null; iter = iter.next) 
             {
                 eventPoster.postEvent (iter.observer, event);
             }
         }
     }
 }

